package com.zhengdongzulin.zuke.app.main.mine.lifeservice

import androidx.lifecycle.rxLifeScope
import com.lxj.androidktx.livedata.StateLiveData
import com.zhengdongzulin.zuke.app.Api
import com.zhengdongzulin.zuke.base.BaseViewModel
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse
import java.io.File

class LifeServiceCreateVm : BaseViewModel() {

    //申请保洁
    var clean_apply: StateLiveData<String> = StateLiveData()
    fun clean_apply(
        houseId: String,
        expectCompletionTime: String,
        repairServiceContent: String,
        renterUploadPicList: String
    ) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.CLEAN_APPLY)
                    .add("houseId", houseId)
                    .add("expectCompletionTime", expectCompletionTime)
                    .add("repairServiceContent", repairServiceContent)
                    .add("renterUploadPicList", renterUploadPicList)
                    .toResponse<String>()
                    .await()
                clean_apply.postValueAndSuccess(r)
            }, {
                assignment(it, clean_apply)
            }
        )
    }

    //申请维修
    var repair_apply: StateLiveData<String> = StateLiveData()
    fun repair_apply(
        houseId: String,
        expectCompletionTime: String,
        repairServiceContent: String,
        renterUploadPicList: String
    ) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.REPAIR_APPLY)
                    .add("houseId", houseId)
                    .add("expectCompletionTime", expectCompletionTime)
                    .add("repairServiceContent", repairServiceContent)
                    .add("renterUploadPicList", renterUploadPicList)
                    .toResponse<String>()
                    .await()
                repair_apply.postValueAndSuccess(r)
            }, {
                assignment(it, repair_apply)
            }
        )
    }

    //申请投诉
    var complaint_letter_apply: StateLiveData<String> = StateLiveData()
    fun complaint_letter_apply(
        houseId: String,
        repairServiceContent: String,
        renterUploadPicList: String
    ) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.COMPLAINT_LETTER_APPLY)
                    .add("houseId", houseId)
                    .add("repairServiceContent", repairServiceContent)
                    .add("renterUploadPicList", renterUploadPicList)
                    .toResponse<String>()
                    .await()
                complaint_letter_apply.postValueAndSuccess(r)
            }, {
                assignment(it, complaint_letter_apply)
            }
        )
    }


    //上传
    var upload: StateLiveData<MutableList<ZKLifeServiceCreateActivity.FileData>> = StateLiveData()
    fun upload(files: File) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postForm(Api.UPLOAD_FILE)
                    .add("fileName", files)
                    .toResponse<MutableList<ZKLifeServiceCreateActivity.FileData>>()
                    .await()
                upload.postValueAndSuccess(r)
            }, {
                assignment(it, upload)
            }
        )
    }

}